//==================================================================
//  Copyright (C) 2006  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
/// @file
///
///
/// 
/// @author Davide Pasca
/// 
//==================================================================

#ifndef RENDSHADERSD3D_H
#define RENDSHADERSD3D_H

#include <xtl.h>
#include "kmatrix.h"
#include "memfile.h"
#include "RendIndexBuffer.h"
#include "RendVertexBuffer.h"
#include "TextureBase.h"

//==================================================================
class RendShadersD3D
{
	D3DDevice						*_d3ddevp;						// cached d3d device from super
	class RendShaders				*_superp;
	char							_datadir[1024];
public:
	com_ptr<D3DVertexDeclaration>	_std_vert_declp[2][2][2];		// vertex format decl
	com_ptr<D3DVertexShader>		_std_vs_programp[2][2][2];		// vertex shaders
	com_ptr<D3DPixelShader>			_std_ps_programp[2][2];			// pixel shaders

	com_ptr<D3DVertexDeclaration>	_part_vert_declarationsp[2];	// vertex format decl
	com_ptr<D3DVertexShader>		_part_vert_shadersp[2];			// particle vertex shaders
	com_ptr<D3DPixelShader>			_part_pix_shadersp[2];			// particle pixel shaders

	com_ptr<D3DVertexShader>		_HDRFunc_T1_C1_VS;
	com_ptr<D3DPixelShader>			_HDRFunc_DS4x4Clamp_PS;
	com_ptr<D3DPixelShader>			_HDRFunc_PlainTexture_PS;
	com_ptr<D3DPixelShader>			_HDRFunc_Bloom_PS;
	com_ptr<D3DPixelShader>			_HDRFunc_FinalPass_PS;

	D3DVertexDeclaration			*_cur_vert_declp;
	D3DVertexShader					*_cur_vert_shaderp;
	D3DPixelShader					*_cur_pix_shaderp;

	RendShadersD3D(void);
	~RendShadersD3D();

	void	Initialize( RendShaders *superp, const char *datadirp );

	void	BeginRender();
	void	EndRender();
	void	ResetState();
	void	SelectStdVShader( int tex, int col, int nor );
	void	SelectStdVDef( int tex, int col, int nor );
	void	SelectPntSprtShader( int col, float max_size );
	void	SelectDownsample4x4ClampShader( u_int src_wd, u_int src_he );
	void	SelectBloomShader(	const Vector4 *samp_offsp,
								const Vector4 *samp_weightsp,
								float bright_threshold );
	void	SelectPlainTextureShader();
	void	SelectHDRFinalPassShader( const Vector4 &bloom_scale );
	void	SelectVertexStream( void *stream_posp, void *stream_texp, void *stream_colp, void *stream_norp, int stride );
	void	SelectVertexBuffer( RendVertexBuffer *vbuffp );
//	void	TouchUniformParam()		{	_cur_vert_shaderp = NULL;	}
//	void	TouchPSUniformParam()	{	_cur_pix_shaderp = NULL;	}

public:
	D3DVertexShader	*CreateD3DVertexShader( const MemFile &memf, const char *entrypointp );
	D3DPixelShader	*CreateD3DPixelShader( const MemFile &memf, const char *entrypointp );
};

//==================================================================
inline void RendShadersD3D::SelectStdVDef( int tex, int col, int nor )
{
	D3DVertexDeclaration	*vert_decp	= _std_vert_declp[tex][col][nor];

	if ( vert_decp != _cur_vert_declp )
		_d3ddevp->SetVertexDeclaration( _cur_vert_declp = vert_decp );
}



#endif
